package com.scwatch.core.web.back.f7;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.User;
import com.scwatch.core.service.UserService;
import com.scwatch.core.support.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by gjl on 2017/7/7.
 */
@Controller
@RequestMapping("/ext/user")
public class UserF7Controller {
    @RequestMapping("anchorchooseUser.do")
    public String anchorchooseUser(HttpServletRequest request, HttpServletResponse response,
                                 org.springframework.ui.Model modelMap) throws IOException {
        return "ext/anchor/anchor_choose_user";
    }

    @RequestMapping("getAnchorUserPage.do")
    @ResponseBody
    public Object getAnchorUserPage(HttpServletRequest request, HttpServletResponse response,
                                  @PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
                                  String name,String startTime,String endTime,String nickName) throws IOException {
        Integer siteId = Context.getCurrentSiteId();
        Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
        params.put("CONTAIN_username",new String[]{name});
        params.put("EQ_status",new String[]{"0"});
        params.put("EQ_group.type",new String[]{"5"});
        params.put("CONTAIN_nickname",new String[]{nickName});

        Page<User> pagedList = userService.findPage(0,null, null, params, pageable);
        List<User> list=pagedList.getContent();
        JSONArray jsonArray=new JSONArray();
        if(list!=null && list.size()>0){
            JSONObject jsonObject=null;
            for(User user:list){
                jsonObject =new JSONObject();
                jsonObject.put("id",user.getId());
                jsonObject.put("name",user.getUsername()==null?"":user.getUsername());
                jsonObject.put("nickName",user.getNickname()==null?"":user.getNickname());
                jsonObject.put("group",user.getGroup().getName()==null?"":user.getGroup().getName());
                jsonArray.add(jsonObject);
            }
        }
        JSONObject o=new JSONObject();
        o.put("totalElements",pagedList.getTotalElements());
        o.put("totalPages",pagedList.getTotalPages());
        o.put("content",jsonArray);
        return o;
    }

    @RequestMapping("livechooseUser.do")
    public String livechooseUser(HttpServletRequest request, HttpServletResponse response,
                                   org.springframework.ui.Model modelMap) throws IOException {
        return "ext/anchor/live_choose_user";
    }

    @RequestMapping("getLiveUserPage.do")
    @ResponseBody
    public Object getLiveUserPage(HttpServletRequest request, HttpServletResponse response,
                                    @PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
                                    String name,String startTime,String endTime,String nickName) throws IOException {
        Integer siteId = Context.getCurrentSiteId();
        Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
        params.put("CONTAIN_username",new String[]{name});
        params.put("EQ_status",new String[]{"0"});
        params.put("IN_group.type",new String[]{"4","5"});
        params.put("CONTAIN_nickname",new String[]{nickName});

        Page<User> pagedList = userService.findPage(0,null, null, params, pageable);
        List<User> list=pagedList.getContent();
        JSONArray jsonArray=new JSONArray();
        if(list!=null && list.size()>0){
            JSONObject jsonObject=null;
            for(User user:list){
                jsonObject =new JSONObject();
                jsonObject.put("id",user.getId());
                jsonObject.put("name",user.getUsername()==null?"":user.getUsername());
                jsonObject.put("nickName",user.getNickname()==null?"":user.getNickname());
                jsonObject.put("group",user.getGroup().getName()==null?"":user.getGroup().getName());
                jsonArray.add(jsonObject);
            }
        }
        JSONObject o=new JSONObject();
        o.put("totalElements",pagedList.getTotalElements());
        o.put("totalPages",pagedList.getTotalPages());
        o.put("content",jsonArray);
        return o;
    }



    @Autowired
    private UserService userService;
}
